package com.example.result

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import com.example.result.dao.api.HttpLoggingInterceptor
import com.example.result.preference.Config
import com.lly.tboc.zhouyi.api.UploadFileApi
import com.lly.tboc.zhouyi.vo.WenYan
import dagger.hilt.android.testing.HiltAndroidRule
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.io.File
import javax.inject.Inject

//@HiltAndroidTest
class ZhouyiDbTest :
    DbTest<com.lly.tboc.zhouyi.db.ZhouyiDb>() {

    @get:Rule(order = 1)
    var instantTaskExecutorRule = InstantTaskExecutorRule()

//    @get:Rule(order = 0)
//    var hiltRule = HiltAndroidRule(this)

    @Inject
    lateinit var uploadFileApi: UploadFileApi

    @Before
    fun init() {
//        hiltRule.inject()
        val instrumentation = InstrumentationRegistry.getInstrumentation()
        val context = instrumentation.targetContext
        uploadFileApi = UploadFileApi.create(Config.getUrl(context), HttpLoggingInterceptor())
    }

    @Test
    fun insert() {
        val instrumentation = InstrumentationRegistry.getInstrumentation()
        val context = instrumentation.targetContext
        val packageName = context.packageName
//         context.startActivity(intent)
        val activityName = MainActivity::class.java.name
        val device = UiDevice.getInstance(instrumentation)
        device.launchActivity(packageName, activityName)

        val data = context.resources.assets.open("zhouyi_jichang.txt")
            .reader().use {
                com.lly.tboc.zhouyi.parseData(it)
            }
        db.transactionExecutor.execute {
            data.forEach {
                db.guaDao().insert(it)
                db.yaoDao().insert(it.data)
                if (it.wenyan.isNotEmpty()) {
                    db.guaDao().insert(WenYan(it.id, it.wenyan))
                    runBlocking {
                        println("${db.guaDao().get(it.id)}")
                        println(it.wenyan)
                    }
                }
            }
        }
        runBlocking {
            db.guaDao().get().first().size shouldBe 64
            db.guaDao().get(1) shouldBe data[0]
        }
//        Thread.sleep(5000)
        runBlocking {
            db.guaDao().get().first().size shouldBe 64
            db.guaDao().get(1) shouldBe data[0]
        }
        device.waitAndFindObject(By.text("乾"))!!.click()
        device.waitAndFindObject(By.textStartsWith("文言"), true)
        device.takeScreenshot()
        device.executeShellCommand("appops set ${context.packageName} android.permission.WRITE_EXTERNAL_STORAGE allow")
        val dbFile = context.getDatabasePath("zhouyi.db")
        upload(dbFile)
    }

    private fun upload(dbFile: File) {
        runBlocking {
            val name = dbFile.name
            val requestFile = dbFile.asRequestBody()
            val formData = MultipartBody.Part.createFormData("file", name, requestFile)
            uploadFileApi.upLoadFile(formData)
        }
    }


    override fun requireDb(): com.lly.tboc.zhouyi.db.ZhouyiDb {
        return com.lly.tboc.zhouyi.db.ZhouyiDb.create(
            ApplicationProvider.getApplicationContext(),
            false
        )
    }
}